package com.borrowed.book.book.controller;

import com.borrowed.book.book.domain.*;
import com.borrowed.book.coupon.entity.SCoupon;
import com.borrowed.book.coupon.service.SCouponService;
import com.borrowed.book.system.controller.BaseController;
import com.borrowed.book.system.domain.AjaxResult;
import com.borrowed.book.system.domain.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "优惠券管理")
@RequestMapping("/api/coupon")
public class CouponController extends BaseController {
    @Autowired
    private SCouponService couponService;

    @ApiOperation(value = "添加优惠券",httpMethod = "POST")
    @PostMapping("/add")
    @ResponseBody
    @Transactional
    public AjaxResult addCoupon(@RequestBody SCoupon coupon){
        AjaxResult result;
        try{
            Boolean flag=couponService.addCoupon(coupon);
            result=AjaxResult.sucess();
        }catch (Exception e){
            e.printStackTrace();
            result=AjaxResult.error(e.getMessage());
        }
        return result;
    }


    @ApiOperation(value = "修改优惠券",httpMethod = "PUT")
    @PutMapping("/update")
    @ResponseBody
    @Transactional
    public AjaxResult updateCoupon(@RequestBody SCoupon coupon){
        AjaxResult result;
        try{
            couponService.updateCoupon(coupon);
            result=AjaxResult.sucess();
        }catch (Exception e){
            e.printStackTrace();
            result=AjaxResult.error(e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "删除优惠券",httpMethod = "DELETE")
    @DeleteMapping("/delete")
    @ApiImplicitParam(name="id",value = "优惠券id",type = "Long",required = true)
    @ResponseBody
    public AjaxResult deleteCoupon(Long id){
        AjaxResult result;
        try{
            Boolean deleteFlag=couponService.deleteCoupon(id);
            result=AjaxResult.sucess();
        }catch (Exception e){
            e.printStackTrace();
            result=AjaxResult.error(e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "后台管理优惠券一览查询",httpMethod = "GET")
    @GetMapping("/select")
    @ResponseBody
    @Transactional
    public TableDataInfo selectCouponList(){
        startPage();
        List<SCoupon> list=couponService.list();
        return getDataTable(list);
    }


    @ApiOperation(value = "根据状态一览查询当前用户优惠券",httpMethod = "GET")
    @GetMapping("/selectUserCoupon")
    @ResponseBody
    @Transactional
    public AjaxResult selectUserCoupon(SelectUserCouponInDto param){
        AjaxResult result;
        try{
            List<SCouponDto> data=couponService.selectUserCoupon(param);
            result=AjaxResult.sucess("操作成功",data);
        }catch (Exception e){
            e.printStackTrace();
            result=AjaxResult.error(e.getMessage());
        }
        return result;
    }
}
